using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcExtExample.Models;
using Newtonsoft.Json;

namespace MvcExtExample.Handlers
{
    /// <summary>
    /// Summary description for LogHistoryOperation
    /// </summary>
    public class UserOperation : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string act = context.Request.Params["act"] ?? "";
            string output = "";
            switch (act.ToLower())
            {
                case "add":
                    output = Add(context.Request);
                    break;
                case "del":
                    output = Del(context.Request);
                    break;
                case "edit":
                    output = Edit(context.Request);
                    break;
                case "read":
                    output = Read();
                    break;
                default:
                    output = "{'success':false,'msg':'错误的操作类型'}";
                    break;
            }
            context.Response.ContentType = "text/javascript";
            context.Response.Write(output);
        }

        private string Read()
        {
            throw new NotImplementedException();
        }

        private string Edit(HttpRequest httpRequest)
        {
            string data = httpRequest.Params["data"] ?? "";
            List<User> logs = new List<User>();
            if (data.Length > 0)
            {
                logs = ProcessData(data);
                try
                {
                    foreach (User item in logs)
                    {
                        string sql = "UPDATE [dbo].[Tb_User] SET [User_Name]='"+item.User_Name+"',Sex='"+item.Sex+"',[Privilege]='"+item.Privilege+"',[ID]='"+item.ID+"',[Cellphone]='"+item.Cellphone+"',[Address]='"+item.Address+"',[Status]='"+item.Status+"' WHERE [User_ID]='" + item.User_ID + "'";
                        framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
                        cs.RunSQL(sql);
                    }
                    return "{'success':true}";
                }
                catch (Exception e)
                {
                    return "{'success':false,'Msg':'" + e.Message + "'}";
                }
            }
            else
            {
                return "{'success':false,'Msg':'错误的提交数据！'}";
            }
        }

        private string Add(HttpRequest httpRequest)
        {
            string data = httpRequest.Params["data"] ?? "";
            List<User> user = new List<User>();
            if (data.Length > 0)
            {
                user = ProcessData(data);
                try
                {
                    foreach (User item in user)
                    {
                        string sql = "INSERT INTO [dbo].[Tb_User] ([User_ID],[Dep_ID],[User_Name],[Sex],[Password],[Privilege],[ID],[Cellphone],[Address],[Status])VALUES ('"+item.User_ID+"','"+item.Dep_ID+"',N'"+item.User_Name+"',N'"+item.Sex+"',N'"+item.Password+"',"+item.Privilege+",'"+item.ID+"','"+item.Cellphone+"',N'"+item.Address+"',N'"+item.Status+"')";
                        framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
                        cs.RunSQL(sql);
                    }
                    return "{'success':true}";
                }
                catch (Exception e)
                {
                    return "{'success':false,'Msg':'" + e.Message + "'}";
                }
            }
            else
            {
                return "{'success':false,'Msg':'错误的提交数据！'}";
            }
        }

        private string Del(HttpRequest httpRequest)
        {
            string data = httpRequest.Params["data"] ?? "";
            List<User> logs = new List<User>();
            if (data.Length > 0)
            {
                logs = ProcessData(data);
                try
                {
                    foreach (User item in logs)
                    {
                        string sql = "DELETE FROM [dbo].[Tb_User] WHERE [User_ID]='" + item.User_ID + "'";
                        framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
                        cs.RunSQL(sql);
                    }
                    return "{'success':true}";
                }
                catch (Exception e)
                {
                    return "{'success':false,'Msg':'" + e.Message + "'}";
                }
            }
            else
            {
                return "{'success':false,'Msg':'错误的提交数据！'}";
            }
        }

        private List<User> ProcessData(string data)
        {
            List<User> logs = new List<User>();
            logs = JsonConvert.DeserializeObject<List<User>>(data);
            return logs;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}